<template>
	<view>
		<view class="edit_time">
			<view class="title">营业时间：</view>
			<view class="nav_time">
				<text :class="item.show?'active':''" v-for="(item,index) in timeList" :key="index" @click="navTimeBtn(index)">{{item.title}}</text>
			</view>
			<view class="title">开始时间：</view>
			<view class="time" :class="start_time?'':'no_color'" @click="timeBtn(1)">
				<view>
					{{start_time || '选择开始时间'}}
				</view>
				<u-icon name="arrow-right" color="#ddd" size="28"></u-icon>
			</view>
			<view class="title">结束时间：</view>
			<view class="time" :class="end_time?'':'no_color'" @click="timeBtn(2)">
				<view>
					{{end_time || '选择结束时间'}}
				</view>
				<u-icon name="arrow-right" color="#ddd" size="28"></u-icon>
			</view>
			<!-- 时间选择器 -->
			<u-picker v-model="show" mode="time" :params="params" @confirm='confirmBtn' confirm-color='#26C281'></u-picker>
		</view>
		<!-- 底部 -->
		<BottomBtn title='提交' @submit='submitBtn'></BottomBtn>
	</view>
</template>

<script>
	import BottomBtn from '@/components/bottom_btn/bottom_btn.vue';
	export default {
		components:{
			BottomBtn
		},
		data() {
			return {
				timeList:[
					{title:'周一',value:1,show:false},
					{title:'周二',value:2,show:false},
					{title:'周三',value:3,show:false},
					{title:'周四',value:4,show:false},
					{title:'周五',value:5,show:false},
					{title:'周六',value:6,show:false},
					{title:'周日',value:7,show:false},
				],
				time_val:[],
				start_time:"",
				end_time:'',
				// 
				type:1,
				show:false,
				params:{
					hour: true,
					minute: true,
				},
			};
		},
		onLoad() {
			var that = this
			that.$tips.showLoading('加载中')
			that.$httpApi.shopConfigIndex().then((res)=>{
				that.$tips.hideLoading()
				if(res.code==1){
					that.start_time=res.data.start_time
					that.end_time=res.data.end_time
					that.time_val=res.data.business.split(',')
					that.showBtn()
				}
			})
		},
		methods:{
			// 
			showBtn(){
				this.timeList.forEach(i=>{
					if(this.time_val.some(item=>item==i.value)){
						i.show=true
					}
				})
			},
			// 提交
			submitBtn(){
				var that = this
				if(that.time_val.length==0){
					that.$tips.showToast('请选择营业时间')
					return
				}else if(that.start_time==''){
					that.$tips.showToast('请选择开始时间')
					return
				}else if(that.end_time==''){
					that.$tips.showToast('请选择结束时间')
					return
				}else{
					that.$tips.showLoading('提交中')
					var params = {
						start_time:that.start_time,
						end_time:that.end_time,
						business:that.time_val.join(','),
					}
					that.$httpApi.shopConfig(params).then((res)=>{
						that.$tips.hideLoading()
						if(res.code==1){
							that.$tips.showToast('提交成功')
						}
					})
				}
			},
			timeChange(){
				this.time_val=[]
				this.timeList.forEach(item=>{
					if(item.show){
						this.time_val.push(item.value)
					}
				})
			},
			// 选择
			navTimeBtn(index){
				this.timeList[index].show=!this.timeList[index].show
				this.timeChange()
			},
			// 选择时间
			timeBtn(val){
				this.type=val
				this.show=true
			},
			confirmBtn(val){
				if(this.type==1){
					this.start_time=val.hour+':'+val.minute
				}
				if(this.type==2){
					this.end_time=val.hour+':'+val.minute
				}
			},
		}
	}
</script>

<style lang="less">
	.edit_time{
		width: 100%;
		padding: 32rpx;
		.title{
			width: 100%;
			font-size: 30rpx;
			color: #333;
			padding-bottom: 20rpx;
		}
		.nav_time{
			width: 100%;
			padding-bottom: 12rpx;
			display: flex;
			flex-wrap: wrap;
			text{
				display: block;
				width: 220rpx;
				margin-bottom: 20rpx;
				margin-right: 12rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 28rpx;
				font-weight: 400;
				color: #666666;
				background: #fff;
				border-radius: 10rpx;
				border: 1rpx solid #ddd;
			}
			text:nth-child(3n){
				margin-right: 0;
			}
			.active{
				color: #26C281;
				background: rgba(38,194,129,0.1);
				border: 1rpx solid #26C281;
			}
		}
		.time{
			padding: 0 20rpx;
			width: 100%;
			height: 80rpx;
			margin-bottom: 32rpx;
			font-size: 28rpx;
			font-weight: 400;
			color: #666666;
			background: #fff;
			border-radius: 10rpx;
			border: 1rpx solid #ddd;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.no_color{
			color: #999;
		}
	}
</style>
